package cn.iocoder.yudao.module.enterprise.controller.admin.praxisProject;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.SortingField;
import cn.iocoder.yudao.framework.common.util.number.NumberUtils;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.enterprise.controller.admin.praxisProject.vo.*;
import cn.iocoder.yudao.module.enterprise.service.praxisProject.EnterpriseProjectService;
import cn.iocoder.yudao.module.member.api.praxisProject.PraxisProjectApi;
import cn.iocoder.yudao.module.member.api.praxisProject.dto.PraxisProjectTypeDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.error;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Api(tags = "管理后台 -企业端- 项目")
@RestController
@RequestMapping("/enterprise/praxis-project")
@Validated
public class EnterprisePraxisProjectController {

    @Resource
    private PraxisProjectApi praxisProjectApi;

    @Resource
    private EnterpriseProjectService enterpriseProjectService;

    @GetMapping("/getPraxisProjectTypeList")
    @ApiOperation("(企业端)获得项目分类")
    @TenantIgnore
    public CommonResult<List<EnterpriseProjectTypeVO>> getPraxisProjectTypeList() {
        List<PraxisProjectTypeDTO> list = praxisProjectApi.getPraxisProjectTypeList();
        List<EnterpriseProjectTypeVO> eList=new ArrayList<>();
        if(CollectionUtils.isNotEmpty(list)){
            list.stream().forEach(m->{
                EnterpriseProjectTypeVO p=new EnterpriseProjectTypeVO();
                BeanUtils.copyProperties(m,p);
                eList.add(p);
            });
        }
        return success(eList);
    }

    @PostMapping("/page")
    @ApiOperation("(企业端)获得项目分页(回收站)")
    @TenantIgnore
    public CommonResult<PageResult<EnterpriseProjectPageRespVO>> getPraxisProjectPage(@Valid @RequestBody EnterpriseProjectPageReqVO pageVO) {
        SortingField sortField = pageVO.getSortField();
        if(null==sortField){
            sortField=new SortingField();
            sortField.setOrder("desc");
            sortField.setField("t.weight");
            pageVO.setSortField(sortField);
        }
        PageResult<EnterpriseProjectPageRespVO> pageResult = enterpriseProjectService.getPraxisProjectPage(pageVO);
        return success(pageResult);
    }

    @GetMapping("/getPraxisProjectList")
    @ApiOperation("项目列表集合(下拉框)")
    @TenantIgnore
    @ApiImplicitParams({
            @ApiImplicitParam(name = "title", value = "项目名称",    dataTypeClass = String.class),
            @ApiImplicitParam(name = "queryType", value = "查询类型",   dataTypeClass = Integer.class)
    })
    public CommonResult<List<EnterpriseProjectPageRespVO>> getPraxisProjectList(String title,Integer queryType) {
        List<EnterpriseProjectPageRespVO> list = enterpriseProjectService.getPraxisProjectList(title,NumberUtils.getInteger(queryType,0));
        return success(list);
    }

    @GetMapping("/get")
    @ApiOperation("(企业端)根据ID获得项目")
    @ApiImplicitParam(name = "id", value = "项目ID", required = true, dataTypeClass = Long.class)
    public CommonResult<EnterpriseProjectVO> getPraxisProjectById(@RequestParam("id") Long id) {
        EnterpriseProjectVO project = enterpriseProjectService.getPraxisProject(id);
        return success(project);
    }

    @GetMapping("/updateShowFlag")
    @ApiOperation("隐藏/显示")
    @ApiImplicitParam(name = "id", value = "项目ID", required = true, dataTypeClass = Long.class)
    @TenantIgnore
    public CommonResult<Long> updateShowFlag(@RequestParam("id") Long id) {
        enterpriseProjectService.updateShowFlag(id);
        return success(1L);
    }


    @PostMapping("/create")
    @ApiOperation("(企业端)创建项目")
    public CommonResult<Long> createPraxisProject(@Valid @RequestBody EnterpriseProjectVO projectVO) {
        Integer num = enterpriseProjectService.getProjectCountByTitle(projectVO.getTitle(), null);
        if(num>0){
            return error("该项目名称已存在,请重新输入");
        }
        List<EnterpriseProjectScheduleVO> scheduleVOList = projectVO.getScheduleVOList();
        String msg = enterpriseProjectService.validateScheduleTime(scheduleVOList,1);
        if(StringUtils.isNotBlank(msg)){
           return error(msg);
        }
        projectVO.setStatus(0);
        projectVO.setExamineFlag("0");
        projectVO.setShowFlag(1);
        return success(enterpriseProjectService.createPraxisProject(projectVO));
    }

    @PostMapping("/update")
    @ApiOperation("(企业端)编辑项目")
    @TenantIgnore
    public CommonResult<Long> updatePraxisProject(@Valid @RequestBody EnterpriseProjectVO projectVO) {
        if(null==projectVO.getId()){
            return error("ID不能为空");
        }
        if(null!=projectVO.getStatus() && 2==projectVO.getStatus()){
            return error("已发布不能编辑");
        }
        Integer num = enterpriseProjectService.getProjectCountByTitle(projectVO.getTitle(), projectVO.getId());
        if(num>0){
            return error("该项目名称已存在,请重新输入");
        }
        List<EnterpriseProjectScheduleVO> scheduleVOList = projectVO.getScheduleVOList();
        String msg = enterpriseProjectService.validateScheduleTime(scheduleVOList,2);
        if(StringUtils.isNotBlank(msg)){
            return error(msg);
        }
        msg = enterpriseProjectService.validateUpdateSchedule(scheduleVOList,projectVO.getId());
        if(StringUtils.isNotBlank(msg)){
            return error(msg);
        }
        if(null==projectVO.getStatus()){
            projectVO.setExamineFlag("0");
            projectVO.setStatus(0);
        }
        enterpriseProjectService.updatePraxisProject(projectVO);
        return success(1L);
    }


    @GetMapping("/submitPraxisProject")
    @ApiOperation("(企业端)发布")
    @ApiImplicitParam(name = "id", value = "项目ID", required = true, dataTypeClass = Long.class)
    public CommonResult<Long> submitPraxisProject(@RequestParam("id") Long id) {
        enterpriseProjectService.submitPraxisProject(id);
        return success(1L);
    }

    @PostMapping("/saveProjectScheduleVO")
    @ApiOperation("(企业端)发布后修改赛程安排")
    @TenantIgnore
    public CommonResult<Boolean> saveProjectScheduleVO(@Valid @RequestBody EnterpriseProjectVO projectVO) {
        if(null==projectVO.getId()){
            return error("项目ID不能为空");
        }
        if(CollectionUtils.isEmpty(projectVO.getScheduleVOList())){
            return error("赛程不能为空");
        }
        enterpriseProjectService.saveProjectScheduleVO(projectVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("(企业端)删除项目")
    @ApiImplicitParam(name = "id", value = "项目ID", required = true, dataTypeClass = Long.class)
    public CommonResult<Boolean> deletePraxisProject(@RequestParam("id") Long id) {
        enterpriseProjectService.deletePraxisProject(id);
        return success(true);
    }

    @PutMapping("/revert")
    @ApiOperation("(企业端)还原项目")
    @ApiImplicitParam(name = "id", value = "项目ID", required = true, dataTypeClass = Long.class)
    public CommonResult<Boolean> revertPraxisProject(@RequestParam("id") Long id) {
        enterpriseProjectService.revertPraxisProject(id);
        return success(true);
    }

    @PutMapping("/getRecordByProjectId")
    @ApiOperation("(企业端)获取项目记录")
    @TenantIgnore
    @ApiImplicitParam(name = "id", value = "项目ID", required = true, dataTypeClass = Long.class)
    public CommonResult<List<EnterpriseProjectRecordVO>> getRecordByProjectId(@RequestParam("id") Long id) {
        List<EnterpriseProjectRecordVO> list = enterpriseProjectService.getRecordByProjectId(id);
        return success(list);
    }

    @PostMapping("/getFeedBackPageByProjectId")
    @ApiOperation("(企业端)获取项目反馈")
    @TenantIgnore
    public CommonResult<PageResult<EnterpriseFeedBackVO>> getFeedBackPageByProjectId(@Valid @RequestBody EnterpriseFeedBackPageReqVO pageVO) {
        PageResult<EnterpriseFeedBackVO> pageResult = enterpriseProjectService.getFeedBackPageByProjectId(pageVO);
        return success(pageResult);
    }


    @GetMapping("/getPraxisProjectCount")
    @ApiOperation("(企业端)实践项目统计")
    @ApiImplicitParam(name = "id", value = "项目ID", required = true, dataTypeClass = Long.class)
    @TenantIgnore
    public CommonResult<EnterpriseProjectCountVO> getPraxisProjectCount(@RequestParam("id") Long id) {
        EnterpriseProjectCountVO project = enterpriseProjectService.getPraxisProjectCount(id);
        return success(project);
    }

  

}
